<template>
	<view class="content">
		<view class="row row-item" data-flag="1" @tap="jump">
			<view class="row-title">提现认证</view>
			<!-- <view class="row-status" :class="{ 'font-color': authStatus }">{{ authStatus ? waiting[waitingIndex]  : '未完善'}}</view> -->
			<view class="row-status" :class="{ 'font-color': authStatus }">{{ authtext}}</view>
			<view class="uni-icon uni-icon-arrowright iocn" />
		</view>
		<view class="row row-item" data-flag="2" @tap="jump">
			<view class="row-title">提现账户</view>
			<view class="row-status" :class="{ 'font-color': bankStatus }">{{ bankName==''? bankStatus ? '已完善' : '未完善':bankName }}</view>
			<view class="uni-icon uni-icon-arrowright iocn" />
		</view>
		<view class="get_amount">
			<view class="get_amount_top">提现金额</view>
			<view class="get_amount_buttom row">
				<view class="amount_iocn">¥</view>
				<input v-model="amount" type="number" />
			</view>
		</view>
		<view class="balance">可用余额{{ balance }}</view>
		<view class="botton-group"><button class="submit-button" :disabled="btnActive" :loading="isShowLoading" @click="getAmount">提现</button></view>
	</view>
</template>

<script>
import { getHcp, withdrawAccountList, withdraw, getWithdraw } from '@/utils/api';
export default {
	data() {
		return {
			authStatus: false,
			bankStatus: false,
			amount: '',
			bankCode:'',
			bankName:'',
			btnActive: false,
			isShowLoading: false,
			balance: '0',
			account: '',
			page: '1',
			limit: '50',
			waiting: ['已完善','审核中', '已拒绝', '未完善'],
			waitingIndex: 0,
			authtext: ''
		};
	},
	watch: {},
	methods: {
		async getWithdraw(){
			let res = await getWithdraw();
			if(res.status == 0){
				this.authtext = res.data.text;
				if(res.data.text == '已完善'){
					this.authStatus = true;
				}else{
					this.authStatus = false;
				}
				this.balance = res.data.withdrawBonus;
			} else {
				uni.showToast({
					icon: 'none',
					title: res.message,
					duration: 1000,
					position: 'bottom'
				});
			}
		},
		async getAmount() {
			if (parseInt(this.amount) == '0' || Math.round(this.amount) != parseFloat(this.amount)) {
				uni.showToast({
					icon: 'none',
					title: '提现金额只能为整数，且最少提现1积分',
					duration: 1000,
					position: 'bottom'
				});
				return;
			}
			if (parseInt(this.amount) > parseInt(this.balance)) {
				uni.showToast({
					icon: 'none',
					title: '申请金额超过可提现金额',
					duration: 1000,
					position: 'bottom'
				});
				return;
			}
			
			if(!this.authStatus){
				uni.showToast({
					icon: 'none',
					title: '请完成提现认证',
					duration: 1000,
					position: 'bottom'
				});
				return;
			}
			
			if(!(this.bankName && this.bankStatus)){
				uni.showToast({
					icon: 'none',
					title: '请完善提现账户',
					duration: 1000,
					position: 'bottom'
				});
				return;
			}
			
			let params = {};
			params['account'] = this.account;
			params['bonus'] = this.amount;
			let res = await withdraw(params);
			if (res.status == 0) {
				this.balance = parseFloat(parseFloat(this.balance) - parseFloat(this.amount)).toFixed(2) ;
				uni.navigateTo({
					url: '../getIntegralResult/getIntegralResult?account=' + this.bankCode + '&bonus=' + this.amount
				});
			} else {
				uni.showToast({
					icon: 'none',
					title: res.message,
					duration: 1000,
					position: 'bottom'
				});
			}
		},
		jump(e) {
			let flag = e.currentTarget.dataset.flag;
			if (flag === '1') {
				if(!this.authStatus || (this.waitingIndex==2)){
					uni.navigateTo({
						url: '../user/info/idcard?single=1'
					});
				}
				
			} else {
				uni.navigateTo({
					url: '../myAccount/myAccount?pageFlag=1'
				});
				
			}
		},
		//提现账户认证
		async withdrawAccountList() {
			this.bankStatus = false;
			let params = {};
			params['page'] = this.page;
			params['limit'] = this.limit;
			let res = await withdrawAccountList(params);
			console.log(res);
			if (res.status == 0) {
				if (res.data.docs.length <1) {
					this.bankStatus = false;
				}else{
					this.bankStatus = true;
				}
			}
		},
		//提现认证
		async getHcp() {
			this.authStatus = false;
			let res = await getHcp();
			console.log(res);
			if (res.status == 0) {
				// uni.setStorageSync('userInfo', res.data);
				if(res.data.approved){
					this.authStatus = true;
					let identification = res.data.identification;
					if (identification.idCard) {
						// 认证过了
						if (identification.idCard.status == 'approved') {
							this.waitingIndex = 0;
						}else if(identification.idCard.status == 'waiting'){
							this.waitingIndex = 1;
						}else if(identification.idCard.status == 'rejected'){
							this.waitingIndex = 2;
						}
					}
				}else{
					this.authStatus = false;
				}
				
			}
		}
	},
	onLoad(opt) {
		this.balance = opt.canWithdrawal;
		console.log(this.balance);
	},
	onShow() {
		this.withdrawAccountList();
		// this.getHcp();
		this.getWithdraw();
		// let account = uni.getStorageSync('bankId');
		// let bankAccount = uni.getStorageSync('bankAccount');
		let bankinfo = uni.getStorageSync('bankinfo');
		console.log(bankinfo.id+'  '+bankinfo.text)
		if (bankinfo) {
			this.bankCode=bankinfo.text;
			this.account = bankinfo.id;
			this.bankName=bankinfo.bank
		} else {
			this.bankCode='';
			this.account = '';
			this.bankName='';
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #f4f4f4;
}

.content {
	border-top: 2upx solid #e3e3e3;
	display: flex;
	flex-direction: column;
}

.row-item {
	height: 113upx;
	background: #ffffff;
	margin-bottom: 20upx;
	padding: 0upx 30upx;

	.row-title {
		width: 118upx;
		height: 113upx;
		font-size: 28upx;
		color: #333333;
		line-height: 113upx;
	}

	.row-status {
		height: 113upx;
		font-size: 28upx;
		color: #999999;
		line-height: 113upx;
		flex: 1;
		text-align: right;

		&.font-color {
			color: #ad85fc;
			// disabled:disabled;
		}
	}

	.iocn {
		color: #333333;
		font-size: 50upx;
	}
}

.get_amount {
	width: 100%;
	height: 160upx;
	background: #ffffff;
	padding: 0 31upx 0 31upx;
	box-sizing: border-box;

	.get_amount_top {
		height: 23upx;
		margin-top: 40upx;
		font-size: 24upx;
		line-height: 1;
		color: #999999;
	}

	.get_amount_buttom {
		margin-top: 19upx;
		color: #333333;
		height: 60upx;

		.amount_iocn {
			line-height: 60upx;
			font-size: 75upx;
		}

		input {
			margin-left: 18upx;
			line-height: 60upx;
			height: 1.3em !important;
			font-size: 65upx;
			min-height: 1.3em !important;
		}
	}
}

.balance {
	width: 100%;
	background: #ffffff;
	border-top: 2upx solid #f4f4f4;
	padding: 0 31upx 0 31upx;
	box-sizing: border-box;
	color: #999999;
	font-size: 24upx;
	line-height: 60upx;
}

.botton-group {
	display: flex;
	flex-direction: column;
	margin: 60upx 30upx 0upx 30upx;

	.submit-button {
		width: 98%;
		height: 88upx;
		background: rgba(173, 133, 252, 1);
		border-radius: 4upx;
		color: #ffffff;
		font-size: 34upx;

		&:after {
			border: none;
		}
	}
}
</style>
